﻿using System;
using System.Web.Mvc;
using System.Linq;
using BabyShop.WebSite.Models;
using BabyShop.WebSite.Services;
using BabyShop.WebSite.ViewModels;

namespace BabyShop.WebSite.Areas.SysManage.Controllers
{
    public class ProductController : BaseController
    {
        private readonly ProductService _productService = new ProductService();
        private readonly EnumServices _enumServices = new EnumServices();
        private readonly SerialNumberService _serialNumberService = new SerialNumberService();

        public ActionResult Index()
        {
            ViewBag.ProductTypeList = _enumServices.GetAllProductTypes().Select(
                p => new SelectListItem
                         {
                             Text = p.ProductTypeName,
                             Value =
                                 p.ProductTypeGuid.
                                 ToString()
                         });
            return View();
        }

        public ActionResult Create()
        {
            ViewBag.ProductTypeList = _enumServices.GetLastProductTypes().Select(p => new SelectListItem
                                                                                          {
                                                                                              Text = p.ProductTypeName,
                                                                                              Value =
                                                                                                  p.ProductTypeGuid.
                                                                                                  ToString()
                                                                                          });
            return View(new Product
                            {
                                ProductGuid = Guid.Empty,
                                ProductCode =
                                    _serialNumberService.GetSerialNumber(DateTime.Now.Date, CurrentUser.UserGuid, "Product")
                            });
        }

        public ActionResult Edit(Guid keyGuid)
        {
            var product = _productService.GetProduct(keyGuid);
            ViewBag.ProductTypeList = _enumServices.GetLastProductTypes().Select(p => new SelectListItem
                                                                                          {
                                                                                              Text = p.ProductTypeName,
                                                                                              Value =
                                                                                                  p.ProductTypeGuid.
                                                                                                  ToString()
                                                                                          });
            ViewBag.ProductPhotoList =
                _productService.GetImgListFromIntro(product.ProductIntro).Select(
                    p => new SelectListItem
                             {
                                 Value = p.Key,
                                 Text = p.Value
                             });
            return View("Create", product);
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Product product)
        {
            bool execSucc = _productService.Save(product, CurrentUser.UserGuid);
            return Json(execSucc
                            ? new AjaxResult {ShowMsg = "保存成功"}
                            : new AjaxResult {ErrorCode = 1001, ShowMsg = "保存失败"});
        }

        [HttpPost]
        public ActionResult ProductList(JqGridViewModel jqGridViewModel)
        {
            string[] searchCols = new[]
                                      {
                                          "ProductGuid", "ProductName", "ProductType.ProductTypeName", "Price",
                                          "CommonColumns.LastModified", "CommonColumns.IsDelete"
                                      };
            return Json(_productService.GetJqGridSearchResult(jqGridViewModel, searchCols, "ProductType"));
        }

        [HttpPost]
        public ActionResult Remove(Guid keyGuid)
        {
            bool execSucc = _productService.Delete(keyGuid, CurrentUser.UserGuid);
            return Json(new AjaxResult { ShowMsg = execSucc ? "删除成功" : "删除失败", ErrorCode = execSucc ? 0 : 1001 });
        }
    }
}
